#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUF_SIZE 1024
#define RLT_SIZE 4
#define OPSZ 4

int main(int argc, char *argv[])
{
    int sock;
    char opmsg[BUF_SIZE];
    int result, op_cnt;
    struct sockaddr_in serv_addr;

    sock = socket(PF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    serv_addr.sin_port = htons((uint16_t)atoi(argv[2]));
    connect(sock, (const struct sockaddr *)&serv_addr, sizeof(serv_addr));

    scanf("%d", &op_cnt);
    opmsg[0]=(char)op_cnt;
    for(int i = 0;i < op_cnt;++i) {
        printf("操作数 %d: ", i + 1);
        scanf("%d", (int *)&opmsg[i * OPSZ + 1]);
    }
    fgetc(stdin);
    fputs("操作符：", stdout);
    scanf("%c", &opmsg[op_cnt * OPSZ + 1]);
    write(sock, opmsg, (size_t)(op_cnt * OPSZ + 2));

    read(sock, &result, RLT_SIZE);
    printf("运算结果： %d\n", result);
    close(sock);
    return 0;
}

